<?php

namespace common\models;

use Yii;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "product".
 *
 * @property integer $id
 * @property string $title
 * @property string $banner_pic
 * @property string $description
 * @property integer $is_brand
 * @property integer $is_home
 * @property string $music
 * @property integer $change_status
 * @property string $pics
 * @property integer $share_num
 * @property integer $read_num
 * @property string $code_url
 * @property integer $sort
 * @property integer $status
 * @property integer $businessId
 * @property integer $created_at
 * @property integer $updated_at
 */
class Product extends \yii\db\ActiveRecord
{
    /** 已删除 */
    const STATUS_DELETED = -1;
    /** 未审批 */
    const STATUS_NO_APPROVAL = 0;
    /** 已审批 */
    const STATUS_APPROVAL = 1;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'product';
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'businessId'], 'required'],
            [['is_brand', 'is_home', 'change_status', 'share_num', 'read_num', 'sort', 'status', 'businessId', 'created_at', 'updated_at'], 'integer'],
            [['pics'], 'string'],
            [['title', 'banner_pic', 'music', 'code_url'], 'string', 'max' => 255],
            [['description'], 'string', 'max' => 1000],
            ['status', 'default', 'value' => self::STATUS_NO_APPROVAL],
            ['status', 'in', 'range' => [self::STATUS_DELETED, self::STATUS_NO_APPROVAL, self::STATUS_APPROVAL]],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'title' => 'Title',
            'banner_pic' => 'Banner Pic',
            'description' => 'Description',
            'is_brand' => 'Is Brand',
            'is_home' => 'Is Home',
            'music' => 'Music',
            'change_status' => 'Change Status',
            'pics' => 'Pics',
            'share_num' => 'Share Num',
            'read_num' => 'Read Num',
            'code_url' => 'Code Url',
            'sort' => 'Sort',
            'status' => 'Status',
            'businessId' => 'Business ID',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        /** 格式化日期 */
        $fields['created_at'] = function(){
            return date('Y-m-d H:i:s', $this->created_at);
        };
        $fields['updated_at'] = function(){
            return date('Y-m-d H:i:s', $this->updated_at);
        };
        return $fields;
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne(['id' => $id]);
    }
}
